package com.atguigu.gmall.publisher.controller;


import com.atguigu.gmall.publisher.beans.TradeProvinceOrderAmount;
import com.atguigu.gmall.publisher.service.TradeStateService;
import com.atguigu.gmall.publisher.utils.DateFormatUtil;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RestController
public class TradeStatesController {

    @Autowired
    private TradeStateService tradeStatesService;

    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date" , defaultValue = "0") Integer date){
        if (date == 0 ){
            //如果请求中没有携带日期参数,将当天作为查询的日期
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatesService.getGMV(date);
        String json = "{\"status\": 0,\"data\": "+ gmv +"}";
        return json;

    }


    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }

    Map resMap = new HashMap();
    resMap.put("status",0);
    Map dataMap =  new HashMap();
    List dataList = new ArrayList();

        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatesService.getProvinceAmount(date);
        for (TradeProvinceOrderAmount provinceOrderAmount : provinceOrderAmountList) {
            Map objMap = new HashMap();
            objMap.put("name",provinceOrderAmount.getProvinceName());
            objMap.put("value",provinceOrderAmount.getOrderAmount());
            dataList.add(objMap);
        }
        dataMap.put("mapData",dataList);
        dataMap.put("valueName","交易域");
        resMap.put("data",dataMap);
        return resMap;
    }

}
